import { defu } from 'defu'
import type { Options, ResolveOptions } from './types'
import { isPackageExists } from './utils'

/// keep-sorted
const defaultOptions = (): ResolveOptions => ({
  command: {
    enable: true,
  },
  gitignore: {},
  imports: {},
  javascript: {},
  n: {},
  prettier: {
    enable: true,
  },
  typescript: {
    enable: true,
  },
  unicon: {},
  vue: {
    enable: true,
    vueBlocks: true,
  },
})

export const resolveOptions = (options: Options): ResolveOptions => {
  const entries = Object.entries(defu(options, defaultOptions())).map(([k, v]) => {
    if (typeof v === 'boolean') {
      return [k, { enable: v }]
    }
    return [k, v]
  })

  const o: ResolveOptions = Object.fromEntries(entries)

  if (!isPackageExists('typescript')) {
    o.typescript.enable = false
  }

  if (!isPackageExists('vue|vitepress|nuxt')) {
    o.vue.enable = false
  }

  if (!isPackageExists('prettier')) {
    o.prettier.enable = false
  }

  return o
}
